package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MomentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMomentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MomentAddLogic {
	return &MomentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MomentAddLogic) MomentAdd(in *userrpc.MomentAddRequest) (*userrpc.MomentAddResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	moment := mysql.Moment{
		UserId:   in.UserId,
		Context:  in.Context,
		Address:  in.Address,
		Resource: in.Resource,
	}

	err := mysql.DB.Debug().Create(&moment).Error

	if err != nil {
		return nil, fmt.Errorf("朋友圈发布失败")
	}
	return &userrpc.MomentAddResponse{Id: int64(moment.ID)}, nil
}
